package com.mju.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mju.dao.StudentMapper;
import com.mju.dao.UsersMapper;
import com.mju.exception.ServiceException;
import com.mju.model.Student;
import com.mju.model.Users;
import com.mju.service.StudentService;

@Service
public class StudentServiceImpl implements StudentService {

	@Autowired
	private StudentMapper studentMapper;
	@Autowired
	private UsersMapper usersMapper;
	@Autowired
	private PasswordEncoder passwordEncoder;
	
	@Value("${custom.defaultpasswd}")
	private String defaultpasswd;
	
	public Page<Student> getList(Student student,int pageNo,int pageSize) {
		Page<Student> page=PageHelper.startPage(pageNo,pageSize);
		studentMapper.selectList(student);
		return page;
	}

	@Override
	@Transactional
	public void addStudent(Student student) throws ServiceException {
		studentMapper.insert(student);
		Users user=new Users();
		user.setStudentno(student.getStudentno());
		user.setPasswd(passwordEncoder.encode(defaultpasswd));
		usersMapper.insert(user);
	}

	@Override
	@Transactional
	public void deleteStudent(String studentno) throws ServiceException {
		studentMapper.deleteByPrimaryKey(studentno);
		usersMapper.deleteByPrimaryKey(studentno);
	}

	@Override
	public Student getStudentById(String studentno) {
		return studentMapper.selectByPrimaryKey(studentno);
	}

	@Override
	public void editStudent(Student student) throws ServiceException {
		studentMapper.updateByPrimaryKey(student);
		
	}
}
